<?php
/**
* @desc Permission Controller
* @visible true
* 
*/
class PermissionController extends BackOfficeController
{
    /**
    * @desc Home
    * @visible true
    */
    public function actionIndex()
    {
        
        $folders = FSM::run('Admin.permission.findFolderTree')->folders;
        Yii::trace('$folders '.CVarDumper::dumpAsString($folders), "Permission");
        
        $this->render('index', array('data'=>$folders));
    }
    
    /**
    * @desc View Permission
    * 
    */
    public function actionEdit()
    {
        $actions = FSM::run('Admin.permission.findActions', array('controller'=>$this->post('controller', '')))->actions;
        $roles = FSM::run('Admin.permission.getRoles')->roles;
        
        $role = '';
        if (count($roles))
        {
            $roles = array_keys($roles);
            $roles = array_combine($roles, $roles);
            $role = current($roles);
        }
        
        $grantedActions = array();
        $actionsSelected = array();
        $inheritSelected = array();
        $role = Yii::app()->request->getParam('role', $role);
        if (empty($role) === false)
        {
            $grantedActions = FSM::_run('Admin.permission.getGrantedActions', array('itemName'=>$role, 'actions'=>$actions))->actions;
            if (count($grantedActions))
            {
                /** @var CDbAuthManager */
                $authManager = Yii::app()->authManager;
                foreach ($grantedActions as $action)
                {
                    if ($authManager->hasItemChild($role, $action))
                    {
                        array_push($actionsSelected, $action);
                    }
                }
                
                $inheritSelected = array_diff($grantedActions, $actionsSelected);
            }
        }
        
        $this->renderPartial('edit', array(
            'roles'=>$roles,
            'actions'=>$actions,
            'grantedActions'=>$grantedActions,
            'inheritSelected'=>$inheritSelected,
            'role'=>$role,
        ));
    }
    
    /**
    * @desc get controllers
    * 
    */
    public function actionControllers()
    {
        $id = $this->post('id', false);
        
        if ($id !== false)
        {
            if (strpos($id, '.') === false)
            {
                $folder = $id.'.controllers';
            }
            else
            {
                list($moduleId, $remain) = explode('.', $id);
                $folder = $moduleId.'.controllers.'.$remain;
            }
            
            $folder = Yii::getPathOfAlias($folder);
            if ($id === 'application')
                $id = '';
            $controllers = FSM::run('Admin.permission.findControllers', array('folder'=>$folder, 'parent_id'=>$id))->controllers;
            //echo CVarDumper::dumpAsString($controllers);
            if (count($controllers))
            {
                echo '<h2>Controllers</h2>';
                $this->widget('zii.widgets.CMenu', array('items'=>$controllers));
            }
        }
    }
    
    public function actionRole()
    {
        $roles = FSM::run('Admin.permission.getRoles')->roles;
        $this->render('roles', array('roles'=>$roles));
    }
    
    public function actionUpdateRole()
    {
        $role = $this->get('role', '');
        
        $authItemForm = new AuthItemForm();
        
        if (isset($_POST['AuthItemForm']))
        {
            $authItemForm->attributes = $_POST['AuthItemForm'];
            if ($authItemForm->validate())
            {
                $isNew = $authItemForm->isNew;
                if ($authItemForm->save())
                {
                    $selected = isset($_POST['parents']) && is_array($_POST['parents']) ? $_POST['parents'] : array();
                    //update parents
                    FSM::run('Admin.permission.updateParents', array('role'=>$authItemForm->name, 'selected'=>$selected));
                    
                    Yii::app()->user->setFlash('success', 'Save role successfully.');
                    $this->redirect(array('updateRole', 'role'=>$authItemForm->name));
                }
            }
        }
        else
        {
            $authItemForm->findRole($role);
        }
        
        $result = FSM::run('Admin.permission.getRolesAvailableParent', array('role'=>$authItemForm->name));
        $parents = $result->roles;
        $selected = $result->selected;
        
        
        $this->renderPartial('update_role', array('model'=>$authItemForm, 'parents'=>$parents, 'selected'=>$selected));
    }
}